package org.dcits.service.impl;

import java.util.List;
import java.util.Map;

import org.dcits.model.Tranflow;
import org.dcits.model.Useracct;
import org.dcits.service.TransferService;
import org.dcits.util.RCodeDict;
import org.dcits.util.JDBCUtil;
import org.dcits.util.ObjectToUseracct;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service

public class TransferServiceImpl implements TransferService {

	@Override
	public Useracct findUseracctByAcctNo(String acctNo) {
		
		String sql = "select user_id,acct_no,acct_salary,ccy,pwd from useracct where acct_no = ?";
		Useracct useracct = null;
		try {
			List<Map<String, Object>> list =  JDBCUtil.query(sql, acctNo);
			if(list.size() == 1) {
				Map<String, Object> map = list.get(0);
				useracct = ObjectToUseracct.objeacToUseracct(map);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return useracct;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String transfer(Tranflow tranflow) throws Exception {
		// 一些重复使用的变量
		
		// 一些初始化参数
		String updateAcctSql = "update useracct set acct_salary = ? where acct_no = ?";
		String insertTranflowSql = "INSERT INTO `tranflow`(a_acct_no, b_acct_no, salary, date, result)"
								 + " VALUES (?, ?, ?, ?, ?)";
		
		// 1. 从 A 账户中扣除余额
		// 1.1 验证账号是否存 检测A余额是否足够
		Useracct aUseracct = findUseracctByAcctNo(tranflow.getaAcctNo());
		if(aUseracct == null) {
			return RCodeDict.DATA_NOT_FOUND;
		}
		// 1.2 验证余额
		double aAcctSalary = aUseracct.getAcctSalary();
		if(aAcctSalary < tranflow.getSalary()) {
			return RCodeDict.NOT_SUFFICIENT_FUNDS;
		}	
		JDBCUtil.update(updateAcctSql, aAcctSalary - tranflow.getSalary(), tranflow.getaAcctNo());	
			
		// 2. 从 B 客户中添加余额
		// 2.1 验证 B 账号
		Useracct bUseracct = findUseracctByAcctNo(tranflow.getbAcctNo());
		if(bUseracct == null) {
			return RCodeDict.DATA_NOT_FOUND;
		}
		double bAcctSalary = bUseracct.getAcctSalary();
		JDBCUtil.update(updateAcctSql, tranflow.getSalary() + bAcctSalary, tranflow.getbAcctNo());	
		
		// 3. 在转账记录表中添加一条转账记录
		
		String remake = this.findUsernameByAcctNo(tranflow.getaAcctNo()) + " 转账给 "
						+ this.findUsernameByAcctNo(tranflow.getbAcctNo()) + " " + tranflow.getSalary() + " 元";
		JDBCUtil.update(insertTranflowSql, tranflow.getaAcctNo(), tranflow.getbAcctNo(),
				tranflow.getSalary(), tranflow.getDate(), remake);
		return RCodeDict.SUCCESS;
	}

	@Override
	public List<Useracct> findUseracctListByacctNo(String acctNo) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Useracct> findUseracctListByUserId(int id) {
		
		String sql = "select * from useracct where user_id = ?";
		List<Map<String, Object>> list = null;
		try {
			list = JDBCUtil.query(sql, id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ObjectToUseracct.objeacToUseracct(list);
	}

	@Override
	public String findUsernameByAcctNo(String acctNo) {
		String sql = "select u.user_name from userinfo u,useracct a where u.user_id = a.user_id and a.acct_no = ?";
		List<Map<String, Object>> list = null;
		try {
			list = JDBCUtil.query(sql, acctNo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(list == null || list.isEmpty()) {
			return null;
		}
		return (String) list.get(0).get("user_name");
	}

	@Override
	public String checkPassword(String acctNo, String password) {
		
		Useracct useracct = findUseracctByAcctNo(acctNo);
		if(useracct == null) {
			return RCodeDict.DATA_NOT_FOUND;
		}
		
		if(password != null && password.equals(useracct.getPwd())) {
			return RCodeDict.PASSWORD_CORRECT;
		}
		System.out.println(password + useracct.getPwd());
		return RCodeDict.PASSWORD_ERROR;
	}

}
